//
// Created by HP on 2024/1/13.
//

#ifndef AURORA_ST_AURORA_FONT_H
#define AURORA_ST_AURORA_FONT_H

#include "common_inc.h"
#include "eflash_driver.h"




//字体管理
class Aurora_Font{
public:
    Aurora_Font(Eflash_Driver *eflash_s=NULL);

    typedef enum FONT_SIZE{
        font_size_big = 0,
        font_size_middle = 1,
        font_size_small = 2
    }FONT_SIZE;

private:
    typedef struct{
        uint16_t min;
        uint16_t max;
        uint8_t  bpp;
        uint8_t  reserved[3];
    }x_header_t;
    typedef struct{
        uint32_t pos;
    }x_table_t;
    typedef struct{
        uint8_t adv_w;  /**< 字符宽度，在此宽度之后绘制下一个字形。8.4 format(存储real_value * 16)。**/
        uint8_t box_w;  /**< 字模宽度                Width of the glyph's bounding box*/
        uint8_t box_h;  /**< 字模高度                Height of the glyph's bounding box*/
        int8_t  ofs_x;  /**< 字模水平方向偏移（右边为正向）      x offset of the bounding box*/
        int8_t  ofs_y;  /**< 字模竖直方向偏移（上边为正向）（当字符需要在基线以下显示时使用这个参数让字模下沉）y offset of the bounding box. Measured from the top of the line*/
        uint8_t r;
    }glyph_dsc_t;

//字体一 22
#define FONT_BIG_BASE_ADDR  0x000000   //22
#define FONT_BIG_SIZE       0x100000   //1M
#define FONT_BIG_HIGHT      39
//字体二 18
#define FONT_MIDDLE_BASE_ADDR  FONT_BIG_BASE_ADDR + FONT_BIG_SIZE   //18
#define FONT_MIDDLE_SIZE       0x100000   //1M
#define FONT_MIDDLE_HIGHT      31
//字体三 14
#define FONT_SMALL_BASE_ADDR  FONT_MIDDLE_BASE_ADDR + FONT_MIDDLE_SIZE   //14
#define FONT_SMALL_SIZE       0x100000   //1M
#define FONT_SMALL_HIGHT      26


//参数存储
#define PARAMETER_UI_STORAGE_BASE_ADDR FONT_SMALL_BASE_ADDR + FONT_SMALL_SIZE
#define PARAMETER_UI_STORAGE_SIZE      0x500 //

#define PARAMETER_FOC_STORAGE_BASE_ADDR PARAMETER_UI_STORAGE_BASE_ADDR + PARAMETER_UI_STORAGE_SIZE
#define PARAMETER_FOC_STORAGE_SIZE      0x500 //

#define PARAMETER_Firmware_STORAGE_BASE_ADDR PARAMETER_FOC_STORAGE_BASE_ADDR + PARAMETER_FOC_STORAGE_SIZE
#define PARAMETER_Firmware_STORAGE_SIZE      0x500 //


//动画的基地址   最大支持66张
#define AIM_IMG_BASE_ADDR   0x800000
#define AIM_IMG_SIZE        0x700000   //7M

//待机图片    最大存储9张图
#define IMG_BASE_ADDR       (AIM_IMG_BASE_ADDR + AIM_IMG_SIZE)
#define IMG_SIZE            0x100000   //1M


#define is_fontdata     0
#define is_aim_imgdata  1
#define is_imgdata      2

public:
    void addEFLASH_Driver(Eflash_Driver *eflash_s);

public:
    uint8_t *getfontData(FONT_SIZE fsize,uint32_t unicode_letter);

    uint8_t *getaimimgData(uint32_t offsize,uint8_t *buff,uint32_t len);
    uint8_t *getimgData(uint32_t offsize,uint8_t *buff,uint32_t len);

public:
    void     delete_font_data(FONT_SIZE fsize);
    void     update_font_data(FONT_SIZE fsize,uint32_t addr_offset,uint8_t *data,uint16_t len);

    void     delete_aim_img_data();
    void     set_update_aim_img_data(uint8_t * data,uint16_t len);


    void     delete_img_data();
    void     set_update_img_data(uint8_t * data,uint16_t len);



    void     set_update_font_type(FONT_SIZE fsize);
    void     set_update_font_data(uint8_t * data,uint16_t len);

    void     set_update_type(uint8_t type);
    uint8_t  get_update_type();


public:
    struct UI_PARAMETER_STORAGE Load_UI_Parameter();
    void Save_UI_Parameter(UI_PARAMETER_STORAGE ui_param);


    void Load_FOC_Parameter();
    void Save_FOC_Parameter(uint8_t sel_M,FOC_PARAMETER_STORAGE foc_param);

    Firmware_PARAMETER_STORAGE Load_Firmware_Parameter();
    void Save_Firmware_Parameter(Firmware_PARAMETER_STORAGE firmware_param);

private:
    uint8_t *__user_font_getdata(uint32_t offset, int size);



public:
    uint8_t font_w,font_h,fontadv_w,font_r,font_bpp;
    int8_t fontofs_x,fontofs_y;
    uint8_t line_height;
public:
    Eflash_Driver *eflash; //外部flash
    x_header_t __g_xbf_hd;
    uint8_t  buff[256];

    //用于数字显示
    uint8_t num_flag;

    uint8_t font_wnum,font_hnum,fontadv_wnum;
    uint8_t font_wdot,font_hdot,fontadv_wdot;
    int8_t fontofs_ynum,fontofs_ydot;

    uint8_t font_wdd,font_hdd,fontadv_wdd;
    uint8_t font_warrow,font_harrow,fontadv_warrow;
    int8_t fontofs_ydd,fontofs_yarrow;
private:
    uint32_t update_font_addr;
    uint32_t update_aim_img_addr;
    uint32_t update_img_addr;
    uint8_t  _update_type;
};
#endif //AURORA_ST_AURORA_FONT_H
